package com.pinyougou.shop.controller;

import com.pinyougou.vo.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import util.FastDFSClient;


@RestController
public class UploadController {

	@Value("${FILE_SERVER_URL}")
	private String file_server_url;

    @RequestMapping("/upload")
    private Result update(MultipartFile file){
        //获取文件全部名称（名称+扩展名）
       String filename= file.getOriginalFilename();
       //获取文件的扩展名，以名称最后一个“.”分割
        String extName=filename.substring(filename.lastIndexOf(".")+1);

        try {
            FastDFSClient fastDFSClient=new FastDFSClient("classpath:config/fdfs_client.conf");
            //文件在文件服务器中的地址
            String fileId=fastDFSClient.uploadFile(file.getBytes(),extName);
            //获取全部地址
            String url=file_server_url+fileId;
            return new Result(true, url);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "上传失败");
        }
    }
	
	
}
